package com.lst.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lst.api.constant.DeleteEnum;
import com.lst.api.entity.LoanType;
import com.lst.api.mapper.LoanTypeMapper;
import com.lst.api.service.ILoanTypeService;
import com.lst.api.vo.CodeAndNameVo;
import com.lst.tools.mybatis.service.impl.BaseService;
import com.lst.tools.mybatis.utils.ExpandSqlUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 贷款种类管理表业务层实现
 *
 * @author Frank
 * @date 2020-02-05 23:28:25
 * @version: V1.0
 */
@Service
public class LoanTypeService extends BaseService<LoanTypeMapper, LoanType> implements ILoanTypeService {

    @Override
    public List<CodeAndNameVo> listCodeAndName() {
        QueryWrapper<LoanType> queryWrapper = new QueryWrapper();
        queryWrapper.eq(LoanType.IS_DELETE, DeleteEnum.DELETE_NO.getKey());
        queryWrapper
                .orderByAsc(ExpandSqlUtils.getIsNullOrderBy(LoanType.SORT, false))
                .orderByAsc(LoanType.SORT)
                .orderByDesc(LoanType.CREATE_AT)
                .orderByDesc(LoanType.ID);
        List<LoanType> loanTypeList = super.list(queryWrapper);
        if (CollectionUtils.isEmpty(loanTypeList)) {
            return Collections.EMPTY_LIST;
        }
        return loanTypeList
                .stream()
                .map(item -> new CodeAndNameVo(item.getId().toString(), item.getTitle()))
                .collect(Collectors.toList());
    }
}